package com.lwj.liwanjia.view.activity.personal;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.PwdBean;
import com.lwj.liwanjia.bean.UpdateHeadBean;
import com.lwj.liwanjia.impl.IsSetPwdView;
import com.lwj.liwanjia.impl.UpdateHeadView;
import com.lwj.liwanjia.presenter.IsSetPwdPresenter;
import com.lwj.liwanjia.presenter.UpdateHeadPresenter;
import com.lwj.liwanjia.utils.BitmapUtils;
import com.lwj.liwanjia.utils.CircleTransform;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.utils.UploadImageDialog;
import com.lwj.liwanjia.view.activity.login.LoginActivity;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import constants.Constants;

public class PersonalActivity extends BaseActivity implements UpdateHeadView, IsSetPwdView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_touxiang)
    ImageView ivTouxiang;
    @BindView(R.id.tv_nicheng)
    TextView tvNicheng;
    @BindView(R.id.rl_edit_zhifu)
    RelativeLayout rlEditzhifu;
    @BindView(R.id.rl_set_zhifu)
    RelativeLayout rlSetzhifu;
    @BindView(R.id.tv_bangding)
    TextView tvBangding;

    private UploadImageDialog headDialog;
    private String imageUrl;
    private String token;
    private UpdateHeadPresenter presenter;
    private String photoUrl;
    private String nicheng;
    private IsSetPwdPresenter isSetPwdPresenter;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("个人资料");
        Intent intent = getIntent();
        photoUrl = intent.getStringExtra("photo");
        nicheng = intent.getStringExtra("nicheng");
        if (!TextUtils.isEmpty(photoUrl) && !TextUtils.isEmpty(nicheng)) {
            Glide.with(this).load(photoUrl).crossFade().error(R.mipmap.touxiang).transform(new CircleTransform(this)).into(ivTouxiang);
            tvNicheng.setText(nicheng);
        }
        token = SpfUtils.getSpfUtils(this).getToken();
        presenter = new UpdateHeadPresenter(this);
        isSetPwdPresenter = new IsSetPwdPresenter(this);
    }

    @Override
    protected void initData() {
        isSetPwdPresenter.isSetPwd(token);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal;
    }

    @OnClick({R.id.tv_left, R.id.rl_touxiang, R.id.rl_nicheng, R.id.rl_mima, R.id.rl_set_zhifu, R.id.rl_edit_zhifu, R.id.rl_bangding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.rl_touxiang://头像
                headDialog = new UploadImageDialog(this);
                headDialog.show();
                break;
            case R.id.rl_nicheng://昵称
                Intent intent = new Intent(this, EditNcActivity.class);
                intent.putExtra("nicheng", tvNicheng.getText().toString());
                startActivityForResult(intent, 4);
                break;
            case R.id.rl_mima://密码修改
                startActivity(new Intent(this, EditPasswordActivity.class).putExtra("title", "修改登录密码"));
                break;
            case R.id.rl_set_zhifu://支付密码设置
                startActivity(new Intent(this, SetzfPwdActivity.class));
                break;
            case R.id.rl_edit_zhifu://支付密码修改
                startActivity(new Intent(this, EditPasswordActivity.class).putExtra("title", "修改支付密码"));
                break;
            case R.id.rl_bangding://手机绑定
//                startActivity(new Intent(this, EditPasswordActivity.class).putExtra("title", "换绑手机"));
                Toast.makeText(this, "暂未开通", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void isSetPwd(PwdBean pwdBean) {
        if (pwdBean.getState() == 0) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        } else if (pwdBean.getState() == 1) {
            if (pwdBean.isData()) {//设置过支付密码
                rlSetzhifu.setVisibility(View.GONE);
                rlEditzhifu.setVisibility(View.VISIBLE);
            } else {
                rlSetzhifu.setVisibility(View.VISIBLE);
                rlEditzhifu.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void updateHead(UpdateHeadBean updateHeadBean) {
        if (updateHeadBean.getState() == 0) {
            Toast.makeText(this, "上传失败", Toast.LENGTH_SHORT).show();
        } else {
            Glide.with(this).load(updateHeadBean.getData()).crossFade().error(R.mipmap.touxiang).transform(new CircleTransform(this)).into(ivTouxiang);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == UploadImageDialog.CAMERA_REQUEST_CODE) {
                imageUrl = headDialog.cameraPath;
                String fileName = System.currentTimeMillis() + ".png";
                BitmapUtils.compressImageByPixel(imageUrl, fileName);
                File file = new File(Constants.IMAGE_PATH + fileName);
                presenter.updateHead(token, file);
            } else if (requestCode == headDialog.ALBUM_REQUEST_CODE) {
                Uri uri = data.getData();
                String[] proj = {MediaStore.Images.Media.DATA};
                //查询本地图片
                Cursor cursor = managedQuery(uri, proj, null, null,
                        null);
                // 这个是获得用户选择的图片的索引值
                int column_index = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                // 将光标移至开头 ，这个很重要，不小心很容易引起越界
                cursor.moveToNext();
                // 最后根据索引值获取图片路径
                imageUrl = cursor.getString(column_index);
                //Glide.with(getActivity()).load(imageUrl).crossFade().transform(new CircleTransform(getActivity())).into(photo);
                String fileName = System.currentTimeMillis() + ".png";
                BitmapUtils.compressImageByPixel(imageUrl, fileName);
                File file = new File(Constants.IMAGE_PATH + fileName);
                presenter.updateHead(token, file);
            } else if (requestCode == 4) {
                tvNicheng.setText(data.getStringExtra("nick"));
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isSetPwdPresenter != null) {
            isSetPwdPresenter = null;
        }
        if (presenter != null) {
            presenter = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
